import {
	getCurrentVersion
} from "@/api/login.js"

export function versionDetection() {
	console.log('App Show', plus.runtime.appid)
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
		let version = widgetInfo.version
		console.log('app version', version)
		getCurrentVersion().then(res => {
			console.log('getCurrentVersion', res)
			if (res && res.current) {
				uni.setStorageSync('current-version', version)
				uni.setStorageSync('latest-version', res.current)
				if (res.current != version) {
					updateSystem()
				}
			}

		})

	})
}



export function updateSystem() {
	console.log('App Update')
	uni.showModal({
		title: '版本信息',
		content: `当前  发现新的应用安装包，点击确定立即更新`,
		success: function(res) {
			if (res.confirm) {
				uni.showLoading({
					title: '更新中……'
				})
				uni.downloadFile({
					// 存放最新安装包的地址
					// http://10.0.21.17:8080/simpleDemo-release.apk
					// http://aksu.suzhidong.cn/longnows/huayouapp/huayou-release.apk
					url: 'http://aksu.suzhidong.cn/longnows/huayouapp/huayou-release.apk',
					success: (downloadResult) => {
						console.log("downloadResult", downloadResult)
						uni.hideLoading();
						if (downloadResult.statusCode === 200) {
							uni.hideLoading();
							plus.runtime.install(downloadResult.tempFilePath, {
								force: false
							}, function() {
								console.log('install success...');
								plus.runtime.quit();
							}, function(e) {
								uni.hideLoading();
								plus.nativeUI.alert("安装失败~")
								console.error('install fail...');
							});

						}

					},
				});
			} else {
				uni.hideLoading();
			}
		},
	})

}